import React from 'react';
import PropTypes from 'prop-types';
import {Button, Form, Row, Col, Card} from 'antd';
import {FORM_OPTIONS} from '../constants';
import {PERMISSION} from './constants';
import WrappedSelect from '../common/WrappedSelect';
import TextInput from 'Shared/components/TextInput';
import DateRangePicker from 'Shared/components/DateRangePicker';
import TextArea from 'Shared/components/TextArea';
import Export from '../common/Export';
import MultipleSelect from '../common/MultipleSelect';
import routes from './routes';
import {isYesNo} from '../Enum';
const FormItem = Form.Item;

const isYesNoList = isYesNo.toList();
class QueryPanel extends React.PureComponent {
    state = {
        data: {},
    }
    handleFilterChange = (value, name) => {
        this.props.onConditionsChange(name, value);
    }

    handleFilterPressEnter = (value, name) => {
        this.props.onConditionsChange(name, value);
        this.props.querySearch();
    }
    render() {
        const {formatMessage} = this.props.intl;
        const {brands} = this.props.initEnumData;
        return (
            <div>
                <Form className="form-standard">
                    <Card>
                        <Row>
                            {brands.length !== 1 && <Col {...FORM_OPTIONS.col}><FormItem label={formatMessage({
                                id: 'queryPanel.form.brandId',
                                defaultMessage: '品牌'
                            })}{...FORM_OPTIONS.item}>
                                <WrappedSelect
                                    allowClear
                                    name="brandId"
                                    value={this.props.conditions.brandId}
                                    options={brands}
                                    onChange={this.handleFilterChange}/>
                            </FormItem>
                            </Col>}
                            <Col {...FORM_OPTIONS.col}>
                                <FormItem label={formatMessage({
                                    id: 'queryPanel.form.vin',
                                    defaultMessage: 'VIN'
                                })}{...FORM_OPTIONS.item}>
                                    <TextArea
                                        name="vin"
                                        placeholder={formatMessage({
                                            id: 'queryPanel.form.vin.placeholder',
                                            defaultMessage: '最多输入1000个VIN，以回车隔开'
                                        })}
                                        value={this.props.conditions.vin}
                                        onBlur={this.handleFilterChange} />
                                </FormItem>
                            </Col>
                            <Col {...FORM_OPTIONS.col}>
                                <FormItem label={formatMessage({
                                    id: 'queryPanel.form.saleDate',
                                    defaultMessage: '实销日期'
                                })}{...FORM_OPTIONS.item}>
                                    <DateRangePicker
                                        name="saleDate"
                                        value={this.props.conditions.saleDate}
                                        onChange={this.handleFilterChange} />
                                </FormItem>
                            </Col>
                            <Col {...FORM_OPTIONS.col}>
                                <FormItem label={formatMessage({
                                    id: 'queryPanel.form.productCode',
                                    defaultMessage: '物料号'
                                })}{...FORM_OPTIONS.item}>
                                    <TextArea
                                        name="productCode"
                                        placeholder={formatMessage({
                                            id: 'queryPanel.form.productCode.placeholder',
                                            defaultMessage: '最多输入1000个物料号，以回车隔开'
                                        })}
                                        value={this.props.conditions.productCode}
                                        onBlur={this.handleFilterChange} />
                                </FormItem>
                            </Col>
                            <Col {...FORM_OPTIONS.col}>
                                <FormItem label={formatMessage({
                                    id: 'queryPanel.form.productName',
                                    defaultMessage: '物料名称'
                                })}{...FORM_OPTIONS.item}>
                                    <TextArea
                                        name="productName"
                                        placeholder={formatMessage({
                                            id: 'queryPanel.form.productName.placeholder',
                                            defaultMessage: '最多输入1000个物料名称，以回车隔开'
                                        })}
                                        value={this.props.conditions.productName}
                                        onBlur={this.handleFilterChange} />
                                </FormItem>
                            </Col>
                            <Col {...FORM_OPTIONS.col}>
                                <FormItem label={formatMessage({
                                    id: 'queryPanel.form.engineCode',
                                    defaultMessage: '发动机号'
                                })}{...FORM_OPTIONS.item}>
                                    <TextInput
                                        name="engineCode"
                                        value={this.props.conditions.engineCode}
                                        onPressEnter={this.handleFilterPressEnter}
                                        onChange={this.handleFilterChange}/>
                                </FormItem>
                            </Col>
                            <Col {...FORM_OPTIONS.col}>
                                <FormItem label={formatMessage({
                                    id: 'queryPanel.form.transmissionSn',
                                    defaultMessage: '变速箱号'
                                })}{...FORM_OPTIONS.item}>
                                    <TextInput
                                        name="transmissionSn"
                                        value={this.props.conditions.transmissionSn}
                                        onPressEnter={this.handleFilterPressEnter}
                                        onChange={this.handleFilterChange}/>
                                </FormItem>
                            </Col>
                            {!this.props.isDealerUser && <Col {...FORM_OPTIONS.col}>
                                <FormItem label={formatMessage({
                                    id: 'queryPanel.form.dealerCode',
                                    defaultMessage: '总代编号'
                                })}{...FORM_OPTIONS.item}>
                                    <TextInput
                                        name="dealerCode"
                                        value={this.props.conditions.dealerCode}
                                        onPressEnter={this.handleFilterPressEnter}
                                        onChange={this.handleFilterChange}/>
                                </FormItem>
                            </Col>}
                            {!this.props.isDealerUser && <Col {...FORM_OPTIONS.col}>
                                <FormItem label={formatMessage({
                                    id: 'queryPanel.form.countryName',
                                    defaultMessage: '国家'
                                })}{...FORM_OPTIONS.item}>
                                    <TextArea
                                        name="countryName"
                                        placeholder={formatMessage({
                                            id: 'queryPanel.form.countryName.placeholder',
                                            defaultMessage: '最多输入1000个国家，以回车隔开'
                                        })}
                                        value={this.props.conditions.countryName}
                                        onBlur={this.handleFilterChange} />
                                </FormItem>
                            </Col>}
                            {!this.props.isDealerUser && <Col {...FORM_OPTIONS.col}>
                                <FormItem label={formatMessage({
                                    id: 'queryPanel.form.cityName',
                                    defaultMessage: '城市'
                                })}{...FORM_OPTIONS.item}>
                                    <TextArea
                                        name="cityName"
                                        placeholder={formatMessage({
                                            id: 'queryPanel.form.cityName.placeholder',
                                            defaultMessage: '最多输入1000个城市，以回车隔开'
                                        })}
                                        value={this.props.conditions.cityName}
                                        onBlur={this.handleFilterChange} />
                                </FormItem>
                            </Col>}
                            <Col {...FORM_OPTIONS.col}>
                                <FormItem label={formatMessage({
                                    id: 'queryPanel.form.rolloutDate',
                                    defaultMessage: '下线时间'
                                })}{...FORM_OPTIONS.item}>
                                    <DateRangePicker
                                        name="rolloutDate"
                                        value={this.props.conditions.rolloutDate}
                                        onChange={this.handleFilterChange} />
                                </FormItem>
                            </Col>
                            <Col {...FORM_OPTIONS.col}>
                                <FormItem label={formatMessage({
                                    id: 'queryPanel.form.productCategoryCode',
                                    defaultMessage: '车型编号'
                                })}{...FORM_OPTIONS.item}>
                                    <MultipleSelect
                                        name="productCategoryCode"
                                        dataOptions={this.props.categories}
                                        value={this.props.conditions.productCategoryCode}
                                        onBlur={this.handleFilterChange} />
                                </FormItem>
                            </Col>
                            <Col {...FORM_OPTIONS.col}>
                                <FormItem
                                    label={this.props.intl.formatMessage({
                                        id: 'queryPanel.form.isSales',
                                        defaultMessage: '是否售前车辆'
                                    })}
                                    {...FORM_OPTIONS.item}>
                                    <WrappedSelect
                                        allowClear
                                        name="isSales"
                                        value={this.props.conditions.isSales}
                                        options={isYesNoList}
                                        onChange={this.handleFilterChange} />
                                </FormItem>
                            </Col>
                            <Col {...FORM_OPTIONS.col}>
                                <FormItem
                                    label={this.props.intl.formatMessage({
                                        id: 'queryPanel.form.isKd',
                                        defaultMessage: '是否KD车辆'
                                    })}
                                    {...FORM_OPTIONS.item}>
                                    <WrappedSelect
                                        allowClear
                                        name="isKd"
                                        value={this.props.conditions.isKd}
                                        options={isYesNoList}
                                        onChange={this.handleFilterChange} />
                                </FormItem>
                            </Col>
                        </Row>
                        <Row>
                            <Col span={6}>
                                <Button key="query" type="primary" onClick={this.props.querySearch} loading={this.props.loading}>{formatMessage({
                                    id: 'queryPanel.btn.query',
                                    defaultMessage: '查询'
                                })}</Button>
                                <Button key="clear" onClick={this.props.onResetClick}>{formatMessage({
                                    id: 'queryPanel.btn.reset',
                                    defaultMessage: '重置'
                                })}</Button>
                            </Col>
                            <Col span={18} className="col-align-right">
                                {this.props.exportable && <Export key={PERMISSION.export} exportRequest={this.props.onClickExport} />}
                                {this.props.exportKdtable && <Export key={PERMISSION.exportKd} exportButton={this.props.intl.formatMessage({
                                    id: 'queryPanel.btn.exportKd',
                                    defaultMessage: 'KD车辆导出'
                                })} exportRequest={this.props.onClickSettlementExport} />}
                                {this.props.importable && <Button key="import" href={`#${routes.import.url()}`}>{formatMessage({
                                    id: 'queryPanel.btn.import',
                                    defaultMessage: 'KD车辆导入'
                                })}</Button>}
                            </Col>
                        </Row>
                    </Card>
                </Form>
            </div>
        );
    }
}

QueryPanel.propTypes = {
    conditions: PropTypes.object.isRequired,
    loading: PropTypes.bool.isRequired,
    querySearch: PropTypes.func.isRequired,
    onClickExport: PropTypes.func.isRequired,
    onClickSettlementExport: PropTypes.func.isRequired,
    onConditionsChange: PropTypes.func.isRequired,
    onResetClick: PropTypes.func.isRequired,
    categories: PropTypes.array,
    intl: PropTypes.object,
    isDealerUser: PropTypes.bool,
};

import {connect} from 'react-redux';
import {saveQueryCondition, querySearch, resetQueryPanel, exportData, exportKdData} from './actions';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
import {injectIntl} from './intl';
import {createSelector} from 'reselect';

const getinitEnumData = createSelector(
    state => state.getIn(['page', 'domainData', 'init']),
    init => {
        const initData = init.toJS();
        const brands = initData.brands ? initData.brands.map(item => ({
            text: item.name,
            value: item.id
        })) : [];
        return {
            brands
        };
    }
);
const getCategoryData = createSelector(
    state => state.getIn(['page', 'domainData', 'category']),
    init => {
        const initData = init.toJS();
        const categories = initData.data ? initData.data.map(item => ({
            code: item.code
        })) : [];
        return categories;
    }
);
const getConditions = selectorFactory(['page', 'appState', 'queryCondition']);
const mapStateToProps = state => ({
    conditions: getConditions(state),
    initEnumData: getinitEnumData(state),
    loading: state.getIn(['page', 'domainData', 'list', 'isFetching']),
    exportable: state.getIn(['page', 'domainData', 'permission', 'data']).includes(PERMISSION.export),
    importable: state.getIn(['page', 'domainData', 'permission', 'data']).includes(PERMISSION.import),
    exportKdtable: state.getIn(['page', 'domainData', 'permission', 'data']).includes(PERMISSION.exportKd),
    isDealerUser: state.getIn(['page', 'domainData', 'init', 'isDealerUser']),
    categories: getCategoryData(state),
});

const mapDispatchToProps = dispatch => ({
    onConditionsChange: (name, value) => dispatch(saveQueryCondition(name, value)),
    querySearch: () => dispatch(querySearch()),
    onResetClick: () => dispatch(resetQueryPanel()),
    onClickExport: () => dispatch(exportData()),
    onClickSettlementExport: () => dispatch(exportKdData()),
});

export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(QueryPanel));
